package com.yds.views.adapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建者：于德水
 * 创建时间：2021/9/11 0011 17:46
 * 创建描述：公用适配器抽象类， YDS RecyclerView Common Adapter
 */
@SuppressWarnings("all")
public abstract class YDSRecyclerViewCAdapter<T> extends RecyclerView.Adapter<YDSViewHolder> {

    protected Context mContext;
    protected List<T> mList;

    private View headerView;
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 2;

    //点击事件
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    /**
     * 添加头部布局
     *
     * @param addHeaderView
     */
    public void addHeaderView(View addHeaderView) {
        if (null == addHeaderView || null != headerView) {
            return;
        }
        this.headerView = addHeaderView;
        notifyItemInserted(0);
    }

    public YDSRecyclerViewCAdapter(Context context) {
        this.mContext = context;
        this.mList = new ArrayList<>();
    }

    @Override
    public YDSViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (null != headerView && viewType == TYPE_HEADER) {
            //如果是正常头
            DisplayMetrics dm = new DisplayMetrics();
            WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            wm.getDefaultDisplay().getMetrics(dm);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(dm.widthPixels, ViewGroup.LayoutParams.WRAP_CONTENT);
            headerView.setLayoutParams(layoutParams);
            return new YDSViewHolder(headerView);
        }

        View view = LayoutInflater.from(mContext).inflate(onCreateViewLayoutID(viewType), parent, false);
        return new YDSViewHolder(view);
    }

    @Override
    public void onViewRecycled(final YDSViewHolder holder) {
        super.onViewRecycled(holder);
    }

    @Override
    public void onBindViewHolder(final YDSViewHolder holder, final int position) {
        ClickEvent(holder, getRealPosition(holder));

        if (getItemViewType(position) == TYPE_HEADER) {
            //如果是头部，不做数据填充
            return;
        } else {
            onBindViewHolders(holder.getViewHolder(), getRealPosition(holder));
        }
    }

    public abstract int onCreateViewLayoutID(int viewType);

    public abstract void onBindViewHolders(BaseViewHolder holder, int position);

    @Override
    public int getItemCount() {
        if (headerView != null) {
            return mList == null ? 0 : mList.size() + 1;
        } else {
            return mList == null ? 0 : mList.size();
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if (null != headerView) {
            if (position == 0) {
                return TYPE_HEADER;
            }
        }
        return TYPE_NORMAL;
    }

    //获取真实的position（与datalist对应，因为添加了空布局，会使得position和data对应不上）
    private int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return headerView == null ? position : position - 1;
    }

    /**
     * 获取列表
     */
    public List<T> getList() {
        return mList;
    }

    /**
     * 添加数据列表到列表头部
     */
    public void addListAtStart(List<T> list) {
        if (list != null && !list.isEmpty()) {
            this.mList.addAll(0, list);
            notifyDataSetChanged();
        }
    }

    /**
     * 添加数据列表到列表尾部
     */
    public void addListAtEnd(List<T> list) {
        if (list != null && !list.isEmpty()) {
            this.mList.addAll(list);
            notifyItemRangeInserted(mList.size() - 1, list.size());
        }
    }

    /**
     * 添加数据列表
     */
    public void addList(List<T> list) {
        if (list != null && !list.isEmpty()) {
            this.mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    /**
     * 添加数据列表 下拉刷新自动清空数据
     */
    public void addListWithPage(List<T> list, int currentPage) {
        if (currentPage == 1) {
            this.mList.clear();
        }
        if (list != null && !list.isEmpty()) {
            this.mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    /**
     * 添加单个元素到列表头
     */
    public void addListBeanAtStart(T t) {
        if (t != null) {
            mList.add(0, t);
            notifyItemInserted(0);
        }
    }

    /**
     * 添加单个元素到列表尾
     */
    public void addListBeanAtEnd(T t) {
        if (t != null) {
            mList.add(t);
            notifyItemInserted(mList.size() - 1);
        }
    }

    /**
     * 替换RecyclerView数据
     */
    public void replaceList(List<T> list) {
        if (list != null) {
            this.mList = list;
        } else {
            mList.clear();
        }

        notifyDataSetChanged();
    }

    /**
     * 替换RecyclerView中的某一个数据
     */
    public void replaceItem(T t, int position) {
        if (position >= 0 && position <= mList.size() && t != null) {
            this.mList.set(position, t);
            notifyItemChanged(position);
        }
    }

    /**
     * 替换某一个元素
     */
    public void replaceBean(int position, T t) {
        if (t != null) {
            this.mList.remove(position);
            this.mList.add(position, t);
            notifyItemChanged(position, t);
        }
    }

    /**
     * 删除RecyclerView所有数据
     */
    public void removeAll() {
        if (mList != null) {
            notifyItemRangeRemoved(0, mList.size());
            this.mList.clear();
        }
    }

    /**
     * 删除RecyclerView指定位置的数据
     */
    public void remove(T t) {
        if (mList != null) {
            this.mList.remove(t);
            notifyDataSetChanged();
        }
    }

    /**
     * 删除RecyclerView指定位置的数据
     */
    public void remove(int position) {
        if (position >= 0 && position <= mList.size() && mList != null) {
            this.mList.remove(position);
            notifyItemRemoved(position);
        }
    }

    /**
     * 点击事件
     */
    public interface OnItemClickListener {

        void setOnItemClickListener(View view, int position);

        void setOnItemLongClickListener(View view, int position);
    }

    protected void ClickEvent(final RecyclerView.ViewHolder holder, final int position) {
        if (mOnItemClickListener != null && position != -1) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.setOnItemClickListener(holder.itemView, position);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.setOnItemLongClickListener(holder.itemView, position);
                    return true;
                }
            });
        }
    }
}
